package com.coocaa.ops.admin.v1.mergeposter.service.impl;

import com.coocaa.ops.admin.v1.mergeposter.mapper.IAiPosterMergeMapper;
import com.coocaa.ops.admin.v1.mergeposter.provider.IRefreshPosterMergeProvider;
import com.coocaa.ops.admin.v1.mergeposter.service.IRefreshPosterMergeService;
import com.coocaa.ops.admin.v1.panel.biz.impl.PanelInfoBizImpl;
import com.coocaa.ops.common.core.base.enums.ResourceTypeEnum;
import com.coocaa.ops.common.core.biz.cache.service.MultiCenterCacheUpdateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author bjh
 * @description
 * @date 2021/1/8 14:28
 */
@Service
public class RefreshPosterMergeServiceImpl implements IRefreshPosterMergeService {
    @Autowired
    private IRefreshPosterMergeProvider refreshPosterMergeProvider;
    @Autowired
    private MultiCenterCacheUpdateService multiCenterCacheUpdateService;
    @Autowired
    private IAiPosterMergeMapper aiPosterMergeMapper;
    @Autowired
    private PanelInfoBizImpl panelInfoBiz;

    @Override
    public void refreshPanelPosterMerge(Integer id, Integer version, boolean checkHasMergePoster) {
        boolean isCurrentVersion = refreshPosterMergeProvider.refreshPanelPosterMerge(id, version, checkHasMergePoster);
        if (isCurrentVersion) {
            // 当前版本，更新缓存
            multiCenterCacheUpdateService.updateMultiCenterCache(id, ResourceTypeEnum.PANEL.getValue());
        }
    }

    @Override
    public void resetPanelPosterMerge(Integer id, Integer version) {
        boolean isCurrentVersion = refreshPosterMergeProvider.resetPanelPosterMerge(id, version);
        if (isCurrentVersion) {
            // 当前版本，更新缓存
            multiCenterCacheUpdateService.updateMultiCenterCache(id, ResourceTypeEnum.PANEL.getValue());
        }
    }

    @Override
    public void resetBlockPosterMerge(Integer blockId) {
        refreshPosterMergeProvider.resetBlockPosterMerge(blockId);
        Integer panelId = aiPosterMergeMapper.queryNormalPanelIdByBlockId(blockId);
        if (null != panelId) {
            multiCenterCacheUpdateService.updateMultiCenterCache(panelId, ResourceTypeEnum.PANEL.getValue());
        } else {
            panelId = aiPosterMergeMapper.queryGroupPanelIdByBlockId(blockId);
            if (null != panelId) {
                multiCenterCacheUpdateService.updateMultiCenterCache(panelId, ResourceTypeEnum.PANEL.getValue());
            }
        }
    }
}
